﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Tank.Properties;

namespace Tank
{
    public partial class gameForm : Form
    {
        #region 游戏要素
        private P1Tank p1Tank = null;
        private Graphics g = null;
        private Random Rness;   //人品器
        #endregion
        
        public gameForm()
        {
            InitializeComponent();
            /* 使用双缓冲 */
            SetStyle(ControlStyles.OptimizedDoubleBuffer
                | ControlStyles.ResizeRedraw
                | ControlStyles.AllPaintingInWmPaint, true);
            Rness = new Random();
        }

        /// <summary>
        /// 加载游戏窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gameForm_Load(object sender, EventArgs e)
        {
            Sound.Play(@"Sound\start.wav");
            GameMana.Instance().InitGameMap(g);//初始化游戏地图
            GameMana.Instance().InitETanke();
            p1Tank = GameMana.Instance().InitPlayer();
        }

        /// <summary>
        /// 窗体关闭进行时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gameForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult result = MessageBox.Show(
                           "你真的玩不下去了吗？",
                           "坦克大战",
                           MessageBoxButtons.YesNo,
                           MessageBoxIcon.Question
                           );

            if (result == DialogResult.No)
            {
                e.Cancel = true;
            }
        }

        /// <summary>
        /// 关闭窗体，退出游戏
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gameForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        /// <summary>
        /// 绘制游戏界面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panel_Paint(object sender, PaintEventArgs e)
        {
            g = e.Graphics;
            GameMana.Instance().DrawGameeElement(g);
        }

        /// <summary>
        /// 按下游戏按键
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gameForm_KeyDown(object sender, KeyEventArgs e)
        {
            GameMana.Instance().Play(e);
        }

        /// <summary>
        /// 释放游戏按键
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gameForm_KeyUp(object sender, KeyEventArgs e)
        {
            p1Tank.KeyUp(e);
        }

        /// <summary>
        /// 刷新游戏面板
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GameTimer_Refresh(object sender, EventArgs e)
        {
            //Invalidate();       
            panel.Invalidate();    //使GamePanel重画            
            GameMana.Instance().CreateThread();//多线程判断游戏元素相遇
            GameMana.Instance().SetEGoToWar();//是指单方坦克参战
        }
        }
    }
